package com.cennavi.minenavidemo.activity.poi;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cennavi.minenavidemo.R;
import com.cennavi.minenavidemo.adapter.PoiAdapter;
import com.cennavi.minenavidemo.presenter.PoiPresenter;
import com.minedata.minenavi.mapdal.PoiItem;
import com.minedata.minenavi.poiquery.DistrictItem;
import com.minedata.minenavi.poiquery.PoiSearch;
import com.minedata.minenavi.poiquery.RegeocodeAddress;
import com.minedata.minenavi.poiquery.Tip;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 周边类型检索
 */
public class PoiTypeActivity extends AppCompatActivity implements PoiPresenter.OnPoiSearchListener {

    private PoiSearch poiSearch = null;
    private PoiSearch.Query query = null;
    private PoiAdapter poiAdapter;
    private List<PoiItem> lists = new ArrayList<>();
    private PoiPresenter mPoiPresenter;

    private Unbinder mUnbinder;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.selectEditText)
    EditText selectEditText;
    @BindView(R.id.deleteImageView)
    ImageView deleteImageView;
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.loadPrevious)
    Button btnLoadPrevious;
    @BindView(R.id.loadNext)
    Button btnLoadNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poitype);
        mUnbinder = ButterKnife.bind(this);
        init();
        initTitlBar();
        initRecyclerView();
    }

    /**
     * 初始化搜索引擎
     */
    private void init() {
        mPoiPresenter = PoiPresenter.getInstance();
        mPoiPresenter.init(PoiTypeActivity.this);
        mPoiPresenter.setOnPoiSearchListener(this);
        mPoiPresenter.initPoiSearch();
    }

    private void initTitlBar() {

        selectEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString().replace(" ",""))) {
                    cancel.setVisibility(View.VISIBLE);
                    deleteImageView.setVisibility(View.VISIBLE);
                } else {
                    cancel.setVisibility(View.GONE);
                    deleteImageView.setVisibility(View.GONE);
                }
            }
        });

        selectEditText.setOnKeyListener((v, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                try {
                    inputMethodManager.hideSoftInputFromWindow(
                            v.getWindowToken(), 0
                    );
                } catch (NullPointerException e) {

                }

                String key = selectEditText.getText().toString().trim();
                onSearch(key);
            }
            return false;
        });

        deleteImageView.setOnClickListener(v -> {
            selectEditText.setText(" ");
            lists.clear();
            poiAdapter.notifyDataSetChanged();
        });

        cancel.setText("搜索");
        cancel.setOnClickListener(v -> {
            String key = selectEditText.getText().toString().trim();
            onSearch(key);
        });
    }

    private void initRecyclerView() {
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        poiAdapter = new PoiAdapter(lists);
        poiAdapter.setListener(info -> showToast(info));
        rv.setAdapter(poiAdapter);
    }

    private void showToast(PoiItem info) {
        Toast.makeText(this, info.toString(), Toast.LENGTH_LONG).show();
    }

    @SuppressLint("WrongConstant")
    private void onSearch(String key) {
        mPoiPresenter.startTypeSearch(key);
    }

    /**
     * 上一页
     */
    @OnClick(R.id.loadPrevious)
    public void loadPrevious() {
        mPoiPresenter.loadPrevious();
    }

    /**
     * 下一页
     *
     */
    @OnClick(R.id.loadNext)
    public void loadNext() {
        mPoiPresenter.loadNext();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mUnbinder.unbind();
        super.onDestroy();
    }

    @Override
    public void onSearchComplete(ArrayList<PoiItem> poiItems, boolean isSuccess) {
        //解析poiResult获取的POI信息
        lists.clear();
        if(isSuccess){
            lists.addAll(poiItems);
        }
        poiAdapter.notifyDataSetChanged();
    }

    @Override
    public void onIdSearch(PoiItem poiItem, boolean isSuccess) {

    }

    @Override
    public void onRegeocodeSearched(RegeocodeAddress regeocodeAddress) {

    }

    @Override
    public void onDistrictSearch(DistrictItem districtItem) {

    }

    @Override
    public void onGetInputtips(List<Tip> tips) {

    }
}
